package org.vectrics.common.html;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.apache.log4j.Category;
import java.security.Principal;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;

import org.vectrics.SystemException;
import org.vectrics.domain.Domain;
import org.vectrics.domain.HtmlCssEntry;
import org.vectrics.domain.HtmlFormType;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.Request;
import org.vectrics.user.Person;




public class CssServlet extends HttpServlet {
    private static final String CONTENT_TYPE = "text/html";
    private static Category log = Category.getInstance(CssServlet.class);
    private boolean initialized = false;

    
    protected void showSessionInformation(Request request) {
		try {
			Domain domain = CoreServiceLocator.getUserAuthService().getSessionDomain(request);
			Person person = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(domain, request);
			if (person == null) {
				log.debug("No authenticated user");
			}
			else {
				log.debug("Authenticated user = " + person.getUser().getUserName());
			}
		}
		catch (Exception ex) {
			log.debug("Exception finding authenticated user.");
		}
		
		//log.info("Request.pathInfo = " + request.getPathInfo());
		log.debug("Request.requestUri = " + request.getRequestURI());
		
    }
    
    /**Initialize global variables*/
    public synchronized void init() throws ServletException {
    	super.init();
        log.info("Document Servlet initializing.");
        initialized = true;
    }

	/**Process the HTTP Get request*/
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);        
	}


	/**Process the HTTP Post request*/
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.service(request, response);        
	}
	
	public void service(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException {
		try {
			log.debug("service() - handling request for PDF report");
	
	
			if (initialized == false) {
				init();
			}
			StringBuffer content = new StringBuffer();
			log.debug("Setting content type");
			response.setContentType("text");
			String domainParam = request.getParameter("domain");
			if (domainParam == null)
				throw new SystemException("Required parameter 'domain' not given");
			Domain domain = CoreServiceLocator.getSystemService().findDomain(new Long(domainParam));

			String htmlFormTypeParam = request.getParameter("formtype");
			if (htmlFormTypeParam == null)
				throw new SystemException("Required parameter 'formtype' not given");
			HtmlFormType formType = CoreServiceLocator.getSystemService().findHtmlFormType(htmlFormTypeParam); 
			
			// TODO - Make sure user has proper credentials to be accessing this resume.
			
			log.debug("Writing bytes to response");
			
			Collection cssEntries = CoreServiceLocator.getSystemService().findCssEntries(domain, formType);
			Iterator iter = cssEntries.iterator();
			while (iter.hasNext()) {
				HtmlCssEntry entry = (HtmlCssEntry)iter.next();
				
			}
			
			OutputStream outStream = response.getOutputStream();
			
			int word = 0;
			outStream.write(content.toString().getBytes());
			int contentLen = content.toString().getBytes().length;
			response.setContentLength(contentLen);
		}
		catch (Exception ve) {
			log.error("VectricsException in service()", ve);
			throw new ServletException("VectricsException in service()", ve);
		}
	}

	private void savePathToSession(HttpServletRequest request) {
		String formPath = request.getRequestURI();
		log.info("formPath = " + formPath);
		HttpSession session = request.getSession();
		session.setAttribute("form-path", formPath);
		session.setAttribute("context-path", request.getContextPath());
		
		log.info("ContextPath = " + session.getAttribute("context-path"));
	}



}